#ifndef _CRT_DEF_H_
#define _CRT_DEF_H_

#include <crt_core.hpp>

#ifdef __cplusplus
extern "C" {
#endif

#ifndef S_SUCCESS
#define S_SUCCESS 0
#endif // !S_SUCCESS

#ifndef S_ERROR
#define S_ERROR 1
#endif // !S_ERROR

#ifndef S_FAILD
#define S_FAILD 2
#endif // !S_FAILD

#ifndef S_FDERROR
#define S_FDERROR 3
#endif // !S_FDERROR

#ifndef S_READERROR
#define S_READERROR 4
#endif // !S_READERROR

#ifndef S_WRITEERROR
#define S_WRITEERROR 5
#endif // !S_WRITEERROR

#ifndef S_OPENERROR
#define S_OPENERROR 6
#endif // !S_OPENERROR

#ifndef S_CONNERROR
#define S_CONNERROR 7
#endif // !S_CONNERROR

#ifndef S_POLLERROR
#define S_POLLERROR 8
#endif // !S_POLLERROR

#ifndef S_SUPORTERROR
#define S_SUPORTERROR 9
#endif // !S_SUPORTERROR

#ifndef S_MEMERROR
#define S_MEMERROR 80
#endif // !S_MEMERROR

#ifndef S_CLOSE
#define S_CLOSE 90
#endif // !S_CLOSE

#ifndef S_PENDING
#define S_PENDING 101
#endif // !S_PENDING

#ifndef S_TIMEOUT
#define S_TIMEOUT 102
#endif // !S_TIMEOUT

#ifndef S_COMPLETED
#define S_COMPLETED 103
#endif // !S_COMPLETED

#ifndef S_FOUND
#define S_FOUND 301
#endif // !S_FOUND

#ifndef S_NOFOUND
#define S_NOFOUND 302
#endif // !S_FOUND

#ifndef S_AGAIN
#define S_AGAIN 303
#endif // !S_AGAIN

#ifndef S_EXIT
#define S_EXIT 304
#endif // !S_EXIT

#ifndef S_RELOAD
#define S_RELOAD 305
#endif // !S_RELOAD

#pragma pack(push, 1)

#ifndef _SafeStringPtr_
#define _SafeStringPtr_
typedef struct StringPtr_s {
    LPCSTR ptr;
    size_t len;
} SafeStringPtr;

typedef const SafeStringPtr* SafeStringConstPtr;
#endif

#ifndef _SafeWStringPtr_
#define _SafeWStringPtr_
typedef struct WStringPtr_s {
    LPCWSTR ptr;
    size_t len;
} SafeWStringPtr;

typedef const SafeWStringPtr* SafeWStringConstPtr;
#endif

#ifndef _SafePStringPtr_
#define _SafePStringPtr_
typedef struct PStringPtr_s {
    LPSTRING ptr;
    size_t len;
} SafePStringPtr;

typedef const SafePStringPtr* SafePStringConstPtr;
#endif

#ifndef _SafeMemoryPtr_
#define _SafeMemoryPtr_
typedef struct MemoryPtr_s {
    const BYTE* ptr;
    size_t len;
} SafeMemoryPtr;

typedef const SafeMemoryPtr* SafeMemoryConstPtr;
#endif

#ifndef buf_ptr
typedef unsigned char* buf_ptr;
#endif

#ifndef buf_len
typedef size_t buf_len;
#endif

#pragma pack(pop)

#define _crt_iswalpha(c) (('A' <= (c) && (c) <= 'Z') || ('a' <= (c) && (c) <= 'z'))

#define _crt_iswdigit(c) (L'0' <= (c) && (c) <= L'9')
#define _crt_isdigit(c)  ('0' <= (c) && (c) <= '9')

#define _crt_tolower(c)  ((((c) >= 'A') && ((c) <= 'Z')) ? ((c) - 'A' + 'a') : (c))
#define _crt_toupper(c)  ((((c) >= 'a') && ((c) <= 'z')) ? ((c) - 'a' + 'A') : (c))
#define _crt_towlower(c) ((((c) >= L'A') && ((c) <= L'Z')) ? ((c) - L'A' + L'a') : (c))
#define _crt_towupper(c) ((((c) >= L'a') && ((c) <= L'z')) ? ((c) - L'a' + L'A') : (c))
#define _max(a, b)       ((a) > (b) ? (a) : (b))
#define _min(a, b)       ((a) < (b) ? (a) : (b))

#if (TARGET_OS == OS_WIN)
#define uu_fast(x) (x)
#define uu_slow(x) (x)

#define _container_of(ptr, type, member) (ptr ? ((type*)(((char*)ptr) - offsetof(type, member))) : NULL)
#elif (TARGET_OS == OS_POSIX)

#if defined __GNUC__ || defined __llvm__
#define uu_fast(x) __builtin_expect((x), 1)
#define uu_slow(x) __builtin_expect((x), 0)
#else
#define uu_fast(x) (x)
#define uu_slow(x) (x)
#endif

// gcc 4.6
#define _container_of(ptr, type, member) \
    ({ \
        const typeof(((type*)0)->member)* __mptr = (ptr); \
        (type*)((char*)__mptr - offsetof(type, member)); \
    })

#elif (TARGET_OS == OS_MACH)

#if defined __GNUC__ || defined __llvm__
#define uu_fast(x) __builtin_expect((x), 1)
#define uu_slow(x) __builtin_expect((x), 0)
#else
#define uu_fast(x) (x)
#define uu_slow(x) (x)
#endif

#define _container_of(ptr, type, member) \
    ({ \
        const typeof(((type*)0)->member)* __mptr = (ptr); \
        (type*)((char*)__mptr - offsetof(type, member)); \
    })

#elif (TARGET_OS == OS_UNIX)

#if defined __GNUC__ || defined __llvm__
#define uu_fast(x) __builtin_expect((x), 1)
#define uu_slow(x) __builtin_expect((x), 0)
#else
#define uu_fast(x) (x)
#define uu_slow(x) (x)
#endif

// gcc 4.6
#define _container_of(ptr, type, member) \
    ({ \
        const typeof(((type*)0)->member)* __mptr = (ptr); \
        (type*)((char*)__mptr - offsetof(type, member)); \
    })

#endif

#ifdef __cplusplus
}
#endif

#endif
